<template>
  <div class="student-archives">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>学员档案</span>
          <el-input
            v-model="searchKeyword"
            placeholder="请输入学员姓名或编号搜索"
            class="search-input"
            clearable
            @keyup.enter="handleSearch"
          >
            <template #append>
              <el-button :icon="Search" @click="handleSearch">搜索</el-button>
            </template>
          </el-input>
        </div>
      </template>

      <el-row :gutter="20" class="archive-list">
        <el-col :span="8" v-for="archive in archives" :key="archive.id">
          <el-card class="archive-card" shadow="hover">
            <div class="archive-header">
              <el-avatar :size="60" :src="archive.avatar" />
              <div class="archive-info">
                <h3>{{ archive.name }}</h3>
                <p>编号：{{ archive.id }}</p>
              </div>
              <el-tag :type="getStatusType(archive.status)">{{ getStatusText(archive.status) }}</el-tag>
            </div>
            
            <el-divider />
            
            <div class="archive-content">
              <el-descriptions :column="2" border>
                <el-descriptions-item label="报名日期">{{ archive.registrationDate }}</el-descriptions-item>
                <el-descriptions-item label="培训类型">{{ archive.courseType }}</el-descriptions-item>
                <el-descriptions-item label="当前科目">{{ archive.currentSubject }}</el-descriptions-item>
                <el-descriptions-item label="教练姓名">{{ archive.coachName }}</el-descriptions-item>
              </el-descriptions>
              
              <div class="progress-section">
                <div class="progress-title">学习进度</div>
                <el-steps :active="getSubjectProgress(archive.progress)" finish-status="success" simple>
                  <el-step title="科目一" />
                  <el-step title="科目二" />
                  <el-step title="科目三" />
                  <el-step title="科目四" />
                </el-steps>
              </div>
              
              <div class="exam-records">
                <div class="exam-title">考试记录</div>
                <el-timeline>
                  <el-timeline-item
                    v-for="record in archive.examRecords"
                    :key="record.date"
                    :type="record.passed ? 'success' : 'danger'"
                    :timestamp="record.date"
                    size="large"
                  >
                    {{ record.subject }} - {{ record.passed ? '通过' : '未通过' }}
                    <template v-if="!record.passed">
                      ({{ record.score }}分)
                    </template>
                  </el-timeline-item>
                </el-timeline>
              </div>
            </div>
            
            <div class="archive-footer">
              <el-button type="primary" @click="viewDetail(archive)">查看详情</el-button>
              <el-button type="success" @click="editArchive(archive)">编辑档案</el-button>
              <el-button type="warning" @click="printArchive(archive)">打印档案</el-button>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[6, 12, 24, 36]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 详情对话框 -->
    <el-drawer
      v-model="detailDrawerVisible"
      title="档案详情"
      direction="rtl"
      size="50%"
      :with-header="true"
    >
      <el-tabs v-model="activeTab">
        <el-tab-pane label="基本信息" name="basic">
          <el-descriptions :column="2" border>
            <el-descriptions-item label="姓名">{{ selectedArchive?.name }}</el-descriptions-item>
            <el-descriptions-item label="性别">{{ selectedArchive?.gender }}</el-descriptions-item>
            <el-descriptions-item label="出生日期">{{ selectedArchive?.birthday }}</el-descriptions-item>
            <el-descriptions-item label="联系电话">{{ selectedArchive?.phone }}</el-descriptions-item>
            <el-descriptions-item label="身份证号">{{ selectedArchive?.idCard }}</el-descriptions-item>
            <el-descriptions-item label="联系地址">{{ selectedArchive?.address }}</el-descriptions-item>
            <el-descriptions-item label="报名日期">{{ selectedArchive?.registrationDate }}</el-descriptions-item>
            <el-descriptions-item label="培训类型">{{ selectedArchive?.courseType }}</el-descriptions-item>
            <el-descriptions-item label="教练姓名">{{ selectedArchive?.coachName }}</el-descriptions-item>
            <el-descriptions-item label="培训状态">{{ getStatusText(selectedArchive?.status) }}</el-descriptions-item>
          </el-descriptions>
        </el-tab-pane>

        <el-tab-pane label="培训记录" name="training">
          <el-table :data="selectedArchive?.trainingRecords" style="width: 100%">
            <el-table-column prop="date" label="培训日期" width="180" />
            <el-table-column prop="subject" label="培训科目" width="120" />
            <el-table-column prop="content" label="培训内容" />
            <el-table-column prop="duration" label="培训时长" width="120" />
            <el-table-column prop="coachComment" label="教练评价" />
          </el-table>
        </el-tab-pane>

        <el-tab-pane label="考试记录" name="exam">
          <el-table :data="selectedArchive?.examRecords" style="width: 100%">
            <el-table-column prop="date" label="考试日期" width="180" />
            <el-table-column prop="subject" label="考试科目" width="120" />
            <el-table-column prop="score" label="考试成绩" width="120" />
            <el-table-column prop="passed" label="是否通过" width="120">
              <template #default="scope">
                <el-tag :type="scope.row.passed ? 'success' : 'danger'">
                  {{ scope.row.passed ? '通过' : '未通过' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" />
          </el-table>
        </el-tab-pane>

        <el-tab-pane label="费用记录" name="payment">
          <el-table :data="selectedArchive?.paymentRecords" style="width: 100%">
            <el-table-column prop="date" label="缴费日期" width="180" />
            <el-table-column prop="type" label="费用类型" width="120" />
            <el-table-column prop="amount" label="金额" width="120">
              <template #default="scope">
                ¥{{ scope.row.amount }}
              </template>
            </el-table-column>
            <el-table-column prop="paymentMethod" label="支付方式" width="120" />
            <el-table-column prop="status" label="状态" width="120">
              <template #default="scope">
                <el-tag :type="scope.row.status === 'paid' ? 'success' : 'warning'">
                  {{ scope.row.status === 'paid' ? '已支付' : '待支付' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" />
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-drawer>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

// 搜索关键词
const searchKeyword = ref('')

// 分页参数
const currentPage = ref(1)
const pageSize = ref(6)
const total = ref(0)

// 档案列表数据
const archives = ref([
  {
    id: '2024001',
    name: '张三',
    avatar: 'https://placeholder.com/150',
    status: 'training',
    registrationDate: '2024-01-15',
    courseType: 'C1手动挡',
    currentSubject: '科目二',
    coachName: '李教练',
    progress: 2,
    examRecords: [
      { date: '2024-02-01', subject: '科目一', passed: true },
      { date: '2024-03-01', subject: '科目二', passed: false, score: 80 }
    ]
  },
  {
    id: '2024002',
    name: '李四',
    avatar: 'https://placeholder.com/150',
    status: 'training',
    registrationDate: '2024-02-10',
    courseType: 'C2自动挡',
    currentSubject: '科目三',
    coachName: '王教练',
    progress: 3,
    examRecords: [
      { date: '2024-03-05', subject: '科目一', passed: true },
      { date: '2024-04-10', subject: '科目二', passed: true },
      { date: '2024-05-15', subject: '科目三', passed: false, score: 85 }
    ]
  },
  {
    id: '2024003',
    name: '王五',
    avatar: 'https://placeholder.com/150',
    status: 'graduated',
    registrationDate: '2023-11-20',
    courseType: 'C1手动挡',
    currentSubject: '已毕业',
    coachName: '张教练',
    progress: 4,
    examRecords: [
      { date: '2023-12-10', subject: '科目一', passed: true },
      { date: '2024-01-20', subject: '科目二', passed: true },
      { date: '2024-02-25', subject: '科目三', passed: true },
      { date: '2024-03-10', subject: '科目四', passed: true }
    ]
  },
  {
    id: '2024004',
    name: '赵六',
    avatar: 'https://placeholder.com/150',
    status: 'training',
    registrationDate: '2024-03-05',
    courseType: 'C1手动挡',
    currentSubject: '科目一',
    coachName: '李教练',
    progress: 1,
    examRecords: []
  },
  {
    id: '2024005',
    name: '钱七',
    avatar: 'https://placeholder.com/150',
    status: 'suspended',
    registrationDate: '2023-12-15',
    courseType: 'C2自动挡',
    currentSubject: '科目二',
    coachName: '王教练',
    progress: 2,
    examRecords: [
      { date: '2024-01-10', subject: '科目一', passed: true },
      { date: '2024-02-20', subject: '科目二', passed: false, score: 75 }
    ]
  },
  {
    id: '2024006',
    name: '孙八',
    avatar: 'https://placeholder.com/150',
    status: 'training',
    registrationDate: '2024-04-01',
    courseType: 'C1手动挡',
    currentSubject: '科目三',
    coachName: '张教练',
    progress: 3,
    examRecords: [
      { date: '2024-05-05', subject: '科目一', passed: true },
      { date: '2024-06-10', subject: '科目二', passed: true },
      { date: '2024-07-15', subject: '科目三', passed: false, score: 90 }
    ]
  }
])

// 详情对话框
const detailDrawerVisible = ref(false)
const activeTab = ref('basic')
const selectedArchive = ref(null)

// 获取状态类型
const getStatusType = (status) => {
  const typeMap = {
    'registered': 'info',
    'training': 'warning',
    'graduated': 'success'
  }
  return typeMap[status] || 'info'
}

// 获取状态文本
const getStatusText = (status) => {
  const textMap = {
    'registered': '已报名',
    'training': '培训中',
    'graduated': '已结业'
  }
  return textMap[status] || status
}

// 获取科目进度
const getSubjectProgress = (progress) => {
  return Math.min(Math.max(progress, 0), 4)
}

// 搜索
const handleSearch = () => {
  // 实现搜索逻辑
}

// 分页大小改变
const handleSizeChange = (val) => {
  pageSize.value = val
  fetchData()
}

// 当前页改变
const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchData()
}

// 获取数据
const fetchData = () => {
  // 实现获取数据逻辑
}

// 查看详情
const viewDetail = (archive) => {
  selectedArchive.value = {
    ...archive,
    gender: '男',
    birthday: '1995-01-01',
    phone: '13800138000',
    idCard: '110101199501011234',
    address: '北京市朝阳区xxx街道',
    trainingRecords: [
      {
        date: '2024-02-15',
        subject: '科目二',
        content: '倒车入库训练',
        duration: '2小时',
        coachComment: '方向盘控制需要加强'
      }
    ],
    paymentRecords: [
      {
        date: '2024-01-15',
        type: '报名费',
        amount: 5800,
        paymentMethod: '微信支付',
        status: 'paid',
        remark: '全额支付'
      }
    ]
  }
  detailDrawerVisible.value = true
}

// 编辑档案
const editArchive = (archive) => {
  // 实现编辑逻辑
}

// 打印档案
const printArchive = (archive) => {
  // 实现打印逻辑
  ElMessage.success('档案打印中...')
}

// 初始化
fetchData()
</script>

<style scoped>
.student-archives {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-input {
  width: 300px;
}

.archive-list {
  margin-bottom: 20px;
}

.archive-card {
  margin-bottom: 20px;
}

.archive-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.archive-info {
  margin-left: 20px;
  flex-grow: 1;
}

.archive-info h3 {
  margin: 0 0 5px;
  font-size: 18px;
}

.archive-info p {
  margin: 0;
  color: #909399;
}

.progress-section,
.exam-records {
  margin-top: 20px;
}

.progress-title,
.exam-title {
  font-weight: bold;
  margin-bottom: 10px;
}

.archive-footer {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>